package com.sephiron.clocksynchronizer;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class CctvListFragment extends ListFragment {
	private static final String TAG = "CctvListFragment";

	private ArrayList<Cctv> mCctvs;
	private Button addCctvButton;

	boolean edited = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

		getActivity().setTitle(R.string.app_name);
		mCctvs = CctvLab.get(getActivity()).getCctvs();

		CctvAdapter adapter = new CctvAdapter(mCctvs);
		setListAdapter(adapter);

		setRetainInstance(true);
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_cctv_list, parent, false);

		addCctvButton = (Button) v.findViewById(R.id.fragment_cctv_add);
		addCctvButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				edited = true;
				
				Cctv cctv = new Cctv();
				CctvLab.get(getActivity()).addCctv(cctv);
				Intent i = new Intent(getActivity(),
						TimeSyncEditModeActivity.class);
				i.putExtra(TimeSyncFragment.EXTRA_CCTV_ID, cctv.getId())
						.putExtra(TimeSyncFragment.EXTRA_EDIT_MODE, true);
				startActivityForResult(i, 0);
			}
		});

		ListView listView = (ListView) v.findViewById(android.R.id.list);
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
			registerForContextMenu(listView);
		} else {
			listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
			listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

				@Override
				public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
					// TODO Auto-generated method stub
					return false;
				}

				@Override
				public void onDestroyActionMode(ActionMode mode) {
					// TODO Auto-generated method stub

				}

				@Override
				public boolean onCreateActionMode(ActionMode mode, Menu menu) {
					MenuInflater inflater = mode.getMenuInflater();
					inflater.inflate(R.menu.cctv_list_item_context, menu);
					return true;
				}

				@Override
				public boolean onActionItemClicked(ActionMode mode,
						MenuItem item) {
					switch (item.getItemId()) {
					case R.id.menu_item_delete_cctv:
						edited = true;
						
						CctvAdapter adapter = (CctvAdapter) getListAdapter();
						CctvLab cctvLab = CctvLab.get(getActivity());
						for (int i = adapter.getCount() - 1; i >= 0; i--) {
							if (getListView().isItemChecked(i)) {
								cctvLab.deleteCctv(adapter.getItem(i));
							}
						}
						mode.finish();
						adapter.notifyDataSetChanged();
						return true;
					case R.id.menu_item_draw_cctv_on_map:
						//TODO: launch map activity
						Intent iToMapActivity = new Intent(getActivity(), CctvMapActivity.class);
//						iToMapActivity.putExtra(HelpActivity.EXTRA_HELP_MODE, HelpActivity.CCTV_LIST_HELP);
						startActivity(iToMapActivity);
					default:
						return false;
					}
				}

				@Override
				public void onItemCheckedStateChanged(ActionMode mode,
						int position, long id, boolean checked) {
				}
			});
		}

		return v;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Cctv c = ((CctvAdapter) getListAdapter()).getItem(position);

		Intent i = new Intent(getActivity(), TimeSyncPagerActivity.class);
		i.putExtra(TimeSyncFragment.EXTRA_CCTV_ID, c.getId());
		startActivity(i);
	}

	private class CctvAdapter extends ArrayAdapter<Cctv> {
		public CctvAdapter(ArrayList<Cctv> cctvs) {
			super(getActivity(), 0, cctvs);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						R.layout.list_item_cctv, null);
			}

			Cctv c = getItem(position);

			TextView nameTextView = (TextView) convertView
					.findViewById(R.id.cctv_list_item_nameTextView);
			nameTextView.setText(c.getCctvName());

			return convertView;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		((CctvAdapter) getListAdapter()).notifyDataSetChanged();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.fragment_cctv_list, menu);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getActivity().getMenuInflater().inflate(R.menu.cctv_list_item_context,
				menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		int position = info.position;
		CctvAdapter adapter = (CctvAdapter) getListAdapter();
		Cctv cctv = adapter.getItem(position);

		switch (item.getItemId()) {
		case R.id.menu_item_delete_cctv:
			edited = true;
			
			CctvLab.get(getActivity()).deleteCctv(cctv);
			adapter.notifyDataSetChanged();
			return true;
		case R.id.menu_item_draw_cctv_on_map:
			//TODO: launch map activity
			Intent iToMapActivity = new Intent(getActivity(), CctvMapActivity.class);
//			iToMapActivity.putExtra(HelpActivity.EXTRA_HELP_MODE, HelpActivity.CCTV_LIST_HELP);
			startActivity(iToMapActivity);
			
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_new_cctv:
			edited = true;
			
			Cctv cctv = new Cctv();
			CctvLab.get(getActivity()).addCctv(cctv);
			Intent iToEdit = new Intent(getActivity(), TimeSyncEditModeActivity.class);
			iToEdit.putExtra(TimeSyncFragment.EXTRA_CCTV_ID, cctv.getId()).putExtra(
					TimeSyncFragment.EXTRA_EDIT_MODE, true);
			startActivityForResult(iToEdit, 0);
			return true;
		case R.id.menu_item_help_cctv_list:
			Intent iToHelpActivity = new Intent(getActivity(), HelpActivity.class);
			iToHelpActivity.putExtra(HelpActivity.EXTRA_HELP_MODE, HelpActivity.CCTV_LIST_HELP);
			startActivity(iToHelpActivity);
			return true;
		case R.id.menu_item_cctv_map:
			//TODO: launch map activity
			Intent iToMapActivity = new Intent(getActivity(), CctvMapActivity.class);
//			iToMapActivity.putExtra(HelpActivity.EXTRA_HELP_MODE, HelpActivity.CCTV_LIST_HELP);
			startActivity(iToMapActivity);
			
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		
		if (edited) {
			CctvLab.get(getActivity()).saveCctvs();	
		}
	}
}
